package test;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.OutputStream;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;

import org.junit.BeforeClass;
import org.junit.Test;
import org.omg.CORBA.portable.InputStream;

import po.Person;

public class Demo {

	private static  EntityManagerFactory unit;

	@BeforeClass
	public static void beforeClass(){
		unit=Persistence.createEntityManagerFactory("JpaTest");
	}
	
	@Test
	public void testSave(){
		Person p1 =new Person(1, "john2", 100);
		
		Person p2 =new Person(2, "carl", 8);
		
		//Person p3 =new Person(6, "carl", 9);
		
		EntityManager em=unit.createEntityManager();
		EntityTransaction tx=em.getTransaction();
		tx.begin();
		try{
			em.persist(p1);
			em.persist(p2);
			//em.persist(p2);
			//em.persist(p3);
			tx.commit();
		}
		catch(Exception e){
			//
			System.out.println("=========");
			e.printStackTrace();
		}
		finally{
			em.close();
		}
		
		
	}
	
	@Test
	public void testSaveimg()throws Exception{

		Person p2 =new Person(2, "carl", 8);
		
		//Person p3 =new Person(6, "carl", 9);
		
		try {
				
			FileInputStream is = new FileInputStream("f:/progress.png");
			byte[] bt = new byte[is.available()];
			is.read(bt);
			p2.setBt(bt);
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		EntityManager em=unit.createEntityManager();
		EntityTransaction tx=em.getTransaction();
		tx.begin();
		try{
		
			em.persist(p2);
			
			tx.commit();
		}
		catch(Exception e){
			//
			System.out.println("=========");
			e.printStackTrace();
		}
		finally{
			em.close();
		}
		
		
		
		
	}
	
	
	@Test
	public void testFind(){
		EntityManager em=unit.createEntityManager();
		Person p = em.find(Person.class, 1);
		System.out.println(p.getPname());
		em.close();
		
	}
	
	@Test
	public void testFindimg(){
		EntityManager em=unit.createEntityManager();
		Person p = em.find(Person.class, 2);
		System.out.println(p.getPname());
		
		byte[] bt= p.getBt();
		try {
			FileOutputStream os=new FileOutputStream("f:/bb.jpg");
			os.write(bt);
			os.close();
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		em.close();
		
	}
	

	@Test
	public void testUpdate(){
		EntityManager em=unit.createEntityManager();
		Person p=em.find(Person.class, 1);
		
		EntityTransaction tx=em.getTransaction();
		tx.begin();
		p.setPname("Carl");
		tx.commit();
		em.close();

	}
	
	@Test
	public void testDelete(){
		EntityManager em=unit.createEntityManager();
		Person p=em.find(Person.class, 1);
		EntityTransaction tx=em.getTransaction();
		tx.begin();
		em.remove(p);
		tx.commit();
		em.close();

	}

	public static void main(String[] args) {
		EntityManagerFactory unit=Persistence.createEntityManagerFactory("JpaTest");
		EntityManager em=unit.createEntityManager();

	}

}
